package com.jf.cloud.user.controller.app;

import com.jf.cloud.user.model.UserTravels;
import com.jf.cloud.user.model.UserTravelsLike;
import com.jf.cloud.user.service.UserTravelsLikeService;
import com.jf.cloud.user.service.UserTravelsService;
import com.jf.cloud.user.vo.UserTravelsLikeVO;
import com.jf.cloud.user.dto.UserTravelsLikeDTO;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author zz
 * @date 2023-11-10 17:18:48
 */
@RestController("appUserTravelsLikeController")
@RequestMapping("/user_travels_like")
@Tag(name = "")
public class UserTravelsLikeController {

    @Autowired
    private UserTravelsLikeService userTravelsLikeService;

    @Autowired
    private UserTravelsService userTravelsService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<UserTravelsLike>> page(@Valid PageDTO pageDTO) {
        PageVO<UserTravelsLike> userTravelsLikePage = userTravelsLikeService.page(pageDTO);
        return ServerResponseEntity.success(userTravelsLikePage);
    }

    @GetMapping("/count")
    @Operation(summary = "点赞/收藏数量", description = "点赞/收藏数量")
    public ServerResponseEntity<Integer> count(@ParameterObject UserTravelsLikeDTO userTravelsLikeDTO) {
        if(userTravelsLikeDTO.getTravelsId()==null){
            return ServerResponseEntity.showFailMsg("记录ID不能为空");
        }
        return ServerResponseEntity.success(userTravelsLikeService.count(userTravelsLikeDTO));
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<UserTravelsLike> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(userTravelsLikeService.getById(id));
    }

    @GetMapping("/getByUserId")
    @Operation(summary = "获取", description = "获取该评论用户是否")
    public ServerResponseEntity<List<UserTravelsLike>> getByUserId(@Valid @ParameterObject UserTravelsLikeDTO userTravelsLikeDTO) {
        Long userId = AuthUserContext.get().getUserId();
        userTravelsLikeDTO.setUserId(userId);
        if(userTravelsLikeDTO.getTravelsId()==null){
            return ServerResponseEntity.showFailMsg("记录ID不能为空");
        }
        return ServerResponseEntity.success(userTravelsLikeService.getByUserId(userTravelsLikeDTO));
    }

    @GetMapping("/listByUserId")
    @Operation(summary = "获取", description = "获取该评论用户是否")
    public ServerResponseEntity<List<UserTravels>> listByUserId(@Valid @ParameterObject UserTravelsLikeDTO userTravelsLikeDTO) {
        Long userId = AuthUserContext.get().getUserId();
        if(userTravelsLikeDTO.getUserId()!=null){
            userTravelsLikeDTO.setUserId(userTravelsLikeDTO.getUserId());
        }else {
            userTravelsLikeDTO.setUserId(userId);
        }
       List<UserTravelsLike> list= userTravelsLikeService.getByUserId(userTravelsLikeDTO);
       List<UserTravels> list1=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            list1.add(userTravelsService.getById(list.get(i).getTravelsId()));
        }
        return ServerResponseEntity.success(list1);
    }

    @GetMapping("/listByOhterUserId")
    @Operation(summary = "获取", description = "获取该评论用户是否")
    public ServerResponseEntity<List<UserTravels>> listByOhterUserId(@Valid @ParameterObject UserTravelsLikeDTO userTravelsLikeDTO) {
        Long userId = AuthUserContext.get().getUserId();
        userTravelsLikeDTO.setUserId(userId);
        List<UserTravelsLike> list= userTravelsLikeService.getByUserId(userTravelsLikeDTO);
        List<UserTravels> list1=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            list1.add(userTravelsService.getById(list.get(i).getTravelsId()));
        }
        return ServerResponseEntity.success(list1);
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Boolean> save(@Valid @RequestBody UserTravelsLikeDTO userTravelsLikeDTO) {
        Long userId = AuthUserContext.get().getUserId();
        userTravelsLikeDTO.setUserId(userId);
        boolean a=true;
        if(userTravelsLikeDTO.getCollect()!=null){
          List<UserTravelsLike> list=  userTravelsLikeService.getByUserId(userTravelsLikeDTO);
          if(list.size()>0){//查出来更新

              UserTravelsLike userTravelsLike = list.get(0);
              userTravelsLike.setCollect(userTravelsLikeDTO.getCollect()==1?2:1);
              userTravelsLikeService.deleteById(userTravelsLike.getId());
              a=false;
          }else{
              UserTravelsLike userTravelsLike = mapperFacade.map(userTravelsLikeDTO, UserTravelsLike.class);
              userTravelsLikeService.save(userTravelsLike);

          }
        }

        if(userTravelsLikeDTO.getStatus()!=null){
            List<UserTravelsLike> list=  userTravelsLikeService.getByUserId(userTravelsLikeDTO);
            if(list.size()>0){//查出来更新
                UserTravelsLike userTravelsLike = list.get(0);
                userTravelsLike.setStatus(userTravelsLikeDTO.getStatus()==1?0:1);
                userTravelsLikeService.deleteById(userTravelsLike.getId());
                a=false;
            }else{
                UserTravelsLike userTravelsLike = mapperFacade.map(userTravelsLikeDTO, UserTravelsLike.class);
                userTravelsLikeService.save(userTravelsLike);

            }
        }
        return ServerResponseEntity.success(a);

    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserTravelsLikeDTO userTravelsLikeDTO) {
        Long userId = AuthUserContext.get().getUserId();
        userTravelsLikeDTO.setUserId(userId);
        UserTravelsLike userTravelsLike = mapperFacade.map(userTravelsLikeDTO, UserTravelsLike.class);
        userTravelsLikeService.update(userTravelsLike);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        userTravelsLikeService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
